<?php

namespace core\lib;

class email
{
	/* Variables */
	private static $servername;
	private static $servermail;
	private static $serverpass;
	private static $serverport;
	private static $serverhost;
	
	private static $timeout;
	private static $auth;
	private static $host;

	private static $sock;

	/* Initialize Function */
	public static function init($auth = true)
	{
		self::$servername = config::get('servername', 'email');
		self::$servermail = config::get("servermail", 'email');
		self::$serverpass = config::get("serverpass", "email");
		self::$serverhost = config::get("serverhost", 'email');
		self::$serverport = config::get("serverport", 'email');
		#
		self::$auth = $auth; //auth
		self::$timeout = 30; //is used in fsockopen() 
		#
		self::$host = "localhost"; //is used in HELO command 
		self::$sock = FALSE;
	}

	/* Main Function */
	public static function sendmail($to, $title, $content, $mailtype = 'HTML')
	{
		$header = "MIME-Version:1.0" . PHP_EOL;
		if($mailtype=="HTML"){
			$header .= "Content-Type:text/html" . PHP_EOL;
		}
		$header .= "To: " . $to . PHP_EOL;
		$header .= "From: " . self::$servername . "<" . self::$servermail . ">" . PHP_EOL;
		$header .= "Subject: " . $title . PHP_EOL;
		$header .= "Date: ".date("r").PHP_EOL;
		$header .= "X-Mailer:By Redhat (PHP/".phpversion().")".PHP_EOL;
		list($msec, $sec) = explode(" ", microtime());
		$header .= "Message-ID: <" . date("YmdHis", $sec) . "." . ($msec*1000000) . "." . self::$servermail. ">" . PHP_EOL;
		$flag1 = self::sockopen_relay($to);
		$flag2 = self::send(self::$host, self::$servermail, $to, $header, $content);
		fclose(self::$sock);
		return $flag1 && $flag2;
	}

	/* Private Functions */
	private static function send($helo, $from, $to, $header, $content = "")
	{
		if (!self::putcmd("HELO", $helo)) {
			return false;
		}
		#auth
		if(self::$auth){
			if (!self::putcmd("AUTH LOGIN", base64_encode(self::$servermail))){
				return false;
			}
			if (!self::putcmd("", base64_encode(self::$serverpass))) {
				return false;
			}
		}
		#
		if(!self::putcmd("MAIL", "FROM:<".$from.">")){
			return false;
		}
		if(!self::putcmd("RCPT", "TO:<".$to.">")){
			return false;
		}
		if(!self::putcmd("DATA")){
			return false;
		}
		fputs(self::$sock, $header . PHP_EOL . $content);
		if (!self::eom()) {
			return false;
		}
		if (!self::putcmd("QUIT")) {
			return false;
		}
		return TRUE;
	}
	
	private static function sockopen_relay()
	{	
		self::$sock = @fsockopen(self::$serverhost, self::$serverport, $errno, $errstr, self::$timeout);
		if(!(self::$sock && self::ok())){
			return FALSE;
		}
		return TRUE;
	}

	private static function eom()
	{
		fputs(self::$sock, PHP_EOL .'.' . PHP_EOL);
		return self::ok();
	}

	private static function ok()
	{
		$response = str_replace(PHP_EOL, "", fgets(self::$sock, 512));
		if(!@ereg("^[23]", $response)){
			fputs(self::$sock, "QUIT" . PHP_EOL);
			fgets(self::$sock, 512);
			return FALSE;
		}
		return TRUE;
	}

	private static function putcmd($cmd, $arg = "")
	{
		if($arg != ""){
			if($cmd=="") $cmd = $arg;
			else $cmd = $cmd." ".$arg;
		}
		fputs(self::$sock, $cmd.PHP_EOL);
		return self::ok();
	}
}
?>